/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.test;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import linkeddata.arida.ufc.br.manager.impl.ExecuteQueryJena;
import org.apache.commons.validator.routines.UrlValidator;

/**
 *
 * @author jeovane
 */
public class QuerySPARQLParameterized {
    public static void main(String[] args) throws Exception {
        List<String> parameterList = new ArrayList<String>();
        String[] schemes = {"http", "https"};
        UrlValidator urlValidator = new UrlValidator(schemes);
        ExecuteQueryJena queryJena = null;
        
//        String splitDelimiter = "drug_name,Fluconazole (Injection, Solution, abc, cba)";
//        String[] split = splitDelimiter.split(",", 2);
//        System.out.println(split[0]);
        
        String query2 = "PREFIX dailymed: <http://www4.wiwiss.fu-berlin.de/dailymed/resource/dailymed/>\n" +
"            PREFIX sider: <http://www4.wiwiss.fu-berlin.de/sider/resource/sider/>\n" +
"            PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
"            PREFIX drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>\n" +
"            PREFIX diseasome: <http://www4.wiwiss.fu-berlin.de/diseasome/resource/diseasome/>\n" +
"            SELECT ?Disease_name ?Drug_name ?Sider_sideEffectName ?Drug_chemicalFormula ?GenericDrug_affectedOrganism\n" +
"            WHERE{\n" +
"                SERVICE <http://localhost:3030/diseasome/sparql> {\n" +
"                    ?ds diseasome:name ?Disease_name.\n" +
"                    FILTER(?Disease_name = ?disease_name)\n" +
"                }\n" +
        
"                SERVICE <http://localhost:3030/dailymed/sparql>{\n" +
"                    ?dm dailymed:fullName ?Drug_name.\n" +
"                    ?dm owl:sameAs ?sa. \n" +
"                    ?dm dailymed:genericDrug ?gdg.\n" +
"                    FILTER (?Drug_name = ?drug_name)\n" +
"                }\n" +
"                SERVICE <http://localhost:3030/sider/sparql> {\n" +
"                    ?sa sider:sideEffect ?se.\n" +
"                    ?se sider:sideEffectName ?Sider_sideEffectName.\n" +
"                }\n" +
"                SERVICE <http://localhost:3030/drugbank/sparql> {\n" +
"                    ?gdg drugbank:chemicalFormula ?Drug_chemicalFormula.\n" +
"                }\n" +
"                OPTIONAL {\n" +
"                    SERVICE <http://localhost:3030/drugbank/sparql> {\n" +
"                        ?gdg drugbank:affectedOrganism ?GenericDrug_affectedOrganism\n" +
"                    }\n" +
"                }\n" +
"            } limit 10";
        
        String query3 = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"            PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
"            PREFIX dailymed: <http://www4.wiwiss.fu-berlin.de/dailymed/resource/dailymed/>\n" +
"            PREFIX drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>\n" +
"            PREFIX sider: <http://www4.wiwiss.fu-berlin.de/sider/resource/sider/>\n" +
"            PREFIX dbpprop: <http://dbpedia.org/property/>\n" +
"\n" +
"            SELECT ?dailymedActiveIngredientName ?siderSideEffectName\n" +
"             ?drugbankChemicalFormula\n" +
"            WHERE {\n" +
"            SERVICE <http://localhost:3030/dailymed/sparql> {\n" +
"             ?ds  dailymed:activeIngredient ?dgai .\n" +
"             ?dgai rdfs:label ?dailymedActiveIngredientName .\n" +
"             ?ds dailymed:genericDrug ?gdg .\n" +
"             ?ds owl:sameAs ?sa .\n" +
"            }\n" +
"            SERVICE <http://localhost:3030/drugbank/sparql> {\n" +
"              ?gdg drugbank:chemicalFormula  ?drugbankChemicalFormula ;\n" +
"            }\n" +
"            OPTIONAL {\n" +
"              SERVICE <http://localhost:3030/sider/sparql> {\n" +
"                ?sa sider:sideEffect ?se .\n" +
"                ?se sider:sideEffectName ?siderSideEffectName .\n" +
"              }\n" +
"            }\n" +
"            }";
        
        String query1 = "PREFIX diseasome: <http://www4.wiwiss.fu-berlin.de/diseasome/resource/diseasome/>\n" +
"            PREFIX dailymed: <http://www4.wiwiss.fu-berlin.de/dailymed/resource/dailymed/>\n" +
"\n" +
"            SELECT ?Disease_name ?Drug_name\n" +
"            WHERE{\n" +
"                SERVICE <http://localhost:3030/diseasome/sparql> {\n" +
"                    ?ds diseasome:name ?Disease_name.\n" +
"                    FILTER(?Disease_name = ?disease_name)\n" +
"                }\n" +
"                SERVICE <http://localhost:3030/dailymed/sparql>{\n" +
"                    ?dm dailymed:fullName ?Drug_name.\n" +
"                } \n" +
"            } limit 10";
        
        String query0 = "PREFIX diseasome: <http://www4.wiwiss.fu-berlin.de/diseasome/resource/diseasome/>\n" +
"            SELECT ?Disease_name\n" +
"            WHERE{\n" +
"                SERVICE <http://localhost:3030/diseasome/sparql> {\n" +
"                   ?Disease diseasome:name ?Disease_name.\n" +
"                } \n" +
"            } limit 10";
        
        String serviceEndpoint = "http://localhost:3030/dailymed/sparql";
        
        //queryJena.setParameterList("disease_name" + "," + "Analbuminemia");
//        parameterList.add("drug_name" + "," + "Fluconazole (Injection, Solution)");
//        parameterList.add("ds" + "," + "http://www4.wiwiss.fu-berlin.de/dailymed/resource/drugs/3056");
        
//        ParameterizedSparqlString sparqlString = new ParameterizedSparqlString(query1);
//        parameterList.add("disease_name" + "," + "Analbuminemia");
        queryJena = new ExecuteQueryJena(serviceEndpoint, query0);
        //queryJena.setParameterList("disease_name" + "," + "Waardenburg syndrome, typ IID, 608890");
        //queryJena.setParameterList("drug_name" + "," + "Terra-Cortril (Suspension)");
        //System.out.println(queryJena.printQueryJSON());
        //queryJena.printQueryConsole();
        
//        for (String s : queryJena.pathBetweenVCDS()) {
//            System.out.println(s);
//        }
        
        //sparqlString.setLiteral("disease_name", "Analbuminemia");
//        sparqlString.setLiteral("drug_name", "Fluconazole (Injection, Solution)");
        //sparqlString.setIri("ds", "http://www4.wiwiss.fu-berlin.de/dailymed/resource/drugs/3056");
        
//        for (int i = 0; i < parameterList.size(); i++) {
//            String[] setLiteral = parameterList.get(i).split(",", 2);
//            if (urlValidator.isValid(setLiteral[1])) {
//                sparqlString.setIri(setLiteral[0], setLiteral[1]);
//            } else {
//                sparqlString.setLiteral(setLiteral[0], setLiteral[1]);
//            }
//        }
//        
//        System.out.println(sparqlString.asQuery());
//        
////        System.out.println(sparqlString.getCommandText());
//        
//        Query query = QueryFactory.create(sparqlString.toString());
//        QueryExecution qe = QueryExecutionFactory.sparqlService(serviceEndpoint,query);
//        
//        try {
//            ResultSet rs = qe.execSelect();
//            if (rs.hasNext()) {
//                System.out.println(ResultSetFormatter.asText(rs));
//            }
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        } finally {
//            qe.close();
//        }
    }
}
